package com.example.task;

import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.entity.mysql.Resourcegroup;
import com.example.entity.yifei.*;
import com.example.mapper.EqtteMapper;
import com.example.mapper.MaintainplanMapper;
import com.example.mapper.MaintainplangroupResourcegroupMapper;
import com.example.mapper.MaintainplanitemMapper;
import com.example.service.EqtteService;
import com.example.service.EqttfService;
import com.example.service.pc.ResourcegroupService;
import com.example.untils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@Order(Integer.MAX_VALUE)
@Slf4j
public class DianJianTask {

    @Autowired
    private MaintainplanMapper maintainplanMapper;
    @Autowired
    private MaintainplanitemMapper maintainplanitemMapper;
    @Autowired
    private MaintainplangroupResourcegroupMapper maintainplangroupResourcegroupMapper;
    @Autowired
    private ResourcegroupService resourcegroupService;
    @Autowired
    private EqtteService eqtteService;
    @Autowired
    private EqtteMapper eqtteMapper;
    @Autowired
    private EqttfService eqttfService;

    // 每天上午7点，下午8点  生產課: 每台機器生成单子
    @Scheduled(cron = "0 0 7,20 * * ?")
    @DSTransactional
    public void sck() {
        genEqtteEqttf("A1-1");
    }

    // 每天上午7点  押出課: 每台機器生成单子
    @Scheduled(cron = "0 0 7 * * ?")
    @DSTransactional
    public void jck() {
        // 1.读出A1-2的所关联的机器，以及项目
        genEqtteEqttf("A1-2");
        // 2.将生成单子
    }

    // 每天上午7点  裝配課: 每台機器生成单子
    @Scheduled(cron = "0 0 7 * * ?")
    @DSTransactional
    public void zpk() {
        // 1.读出B1-1的所关联的机器，以及项目
        genEqtteEqttf("B1-1");
        // 2.将生成单子
    }

    /**
     * 通过组编号ID生成一级保养单
     * @param mpId 组编号
     */
    public void genEqtteEqttf(String mpId) {
        // 1.读出A1-1的所关联的机器，以及项目
        Maintainplan maintainplan = maintainplanMapper.selectOne(new LambdaQueryWrapper<Maintainplan>()
                .eq(Maintainplan::getMpid, mpId)
                .eq(Maintainplan::getMpstatus, "开启"));
        if(maintainplan == null) return;
        List<Maintainplanitem> maintainplanitemList = maintainplanitemMapper.selListByMpuuid(maintainplan.getMpuuid());
        // 如果找不到A1-1, 或者是关闭状态直接return
        if(maintainplanitemList == null || maintainplanitemList.size() == 0) return;
        // 查询对应的保养项目
        List<Maintainplanitem> itemList = maintainplanitemMapper.selListByMpuuid(maintainplan.getMpuuid());
        // 如果没有配置保养项目，直接return
        if(itemList == null || itemList.size() == 0) return;
        // 拿到所有机器组
        List<MaintainplangroupResourcegroup> resourcegroupList = maintainplangroupResourcegroupMapper.selectList(
                new LambdaQueryWrapper<MaintainplangroupResourcegroup>()
                        .eq(MaintainplangroupResourcegroup::getMpguuid, maintainplan.getMpgfk()));
        // 2.将生成单子
        // 2.1 循环机器的资源组
//        List<Eqtte> eqtteList = new ArrayList<>();
//        List<Eqttf> eqttfList = new ArrayList<>();
        for(MaintainplangroupResourcegroup resourcegroup : resourcegroupList) {
            // 2.2拿出所有的具体机器
            List<Resourcegroup> list = resourcegroupService.list(new LambdaQueryWrapper<Resourcegroup>().eq(Resourcegroup::getResourceGroupParentId, resourcegroup.getResourcegroupid()));
            for(Resourcegroup device : list) {
                Eqtte eqtte = new Eqtte();
                eqtte.setTE001("BYD");
                eqtte.setTE002(eqtteMapper.selTe002(eqtte.getTE001()));
                eqtte.setTE003(TimeUtils.HZStdTime());
                eqtte.setTE004("1");  // 0.维修单 1.保养单 2.点检单
                eqtte.setTE008("1");  // 1设备  2生产线  3模具  4工装
                eqtte.setTE010(device.getResourceGroupId().toString());
                eqtte.setTE011(device.getResourceGroupNO());
                eqtte.setTE012(device.getResourceGroupName());
                eqtte.setTE019("0");
                eqtte.setTE027("每日自动生成一级保养单。时间：" + TimeUtils.getDayTime());
                eqtte.setTE028(maintainplan.getMpuuid().toString());  // 计划UUID
                eqtte.setTE029(maintainplan.getMpid());  // 计划编号
                eqtte.setTE030(maintainplan.getMpname());  // 计划名称
                eqtte.setTE031(TimeUtils.HZStdTime());
                eqtte.setTE032(TimeUtils.HZStdTime());
                eqtte.setTE059(device.getResourceGroupId().toString());
                eqtte.setTE060(device.getResourceGroupNO());
                eqtte.setTE061(device.getResourceGroupName());
                eqtte.setCREATE_DATE(TimeUtils.getFullNowTime());
                eqtte.setCREATOR("ERP");
                eqtteMapper.insert(eqtte);
//                eqtteList.add(eqtte);
                int idx = 1;
                List<Eqttf> eqttfList = new ArrayList<>();
                for(Maintainplanitem item : itemList) {
                    Eqttf eqttf = new Eqttf();
                    eqttf.setTF001(eqtte.getTE001());
                    eqttf.setTF002(eqtte.getTE002());
                    eqttf.setTF003(""+idx);  idx++;
                    eqttf.setUDF01(item.getMpiid());  // 维护编号
                    eqttf.setUDF02(item.getMpiname());  // 维护名称
                    eqttf.setTF005(item.getMpicontent());  // 维护内容
                    eqttf.setTF008(item.getMpiresulttype());  // 结果类型 1.文字 2.数值 3.日期[DEF:1]
                    eqttf.setCREATE_DATE(TimeUtils.getNowTime());
                    eqttf.setCREATOR("ERP");
                    eqttfList.add(eqttf);
//                    eqttfMapper.insert(eqttf);
                }
                eqttfService.saveBatch(eqttfList);
            }
        }
    }

}
